layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage', 'upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;
	var where_json = {
		_o: 'jd_id',
		_b: 'desc'
	};

	var table_filter = 'jdgoods_list',
		form_filter = 'jdgoods_search_form';
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: jdgoods_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'jd_id',
			_b: 'desc'
		},
		cols: [jdgoods_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 16,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#jdgoodsToolbar',
		defaultToolbar: ['filter'],
		initSort: {
			field: 'jd_id',
			type: 'desc'
		}
	});

	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch (obj.event) {
			case 'collectJdGoods':
				layer.open({
					type: 2,
					anim: -1,
					title: '采集京东商品',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: jdgoods_collect_url
				})
				break;
			default:
				break;
		}
		return false;
	})

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var goods_sign = data.goods_sku;
		var jdgoods_id = data.jd_id;
		switch (e) {
			case 'edit':
				create_tab($(this));
				break;
			default:
				break;
		}
	});
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		where_json[name] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});
})
